package com.jcrj.service;

import com.jcrj.dao.HolidayDao;
import com.jcrj.entity.OHoliday;
import com.jcrj.entity.OLeave;
import com.jcrj.entity.OUser;
import com.jcrj.util.DateUtil;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class HolidayService
{
  private HolidayDao holidayDao;

  public HolidayDao getHolidayDao()
  {
    return this.holidayDao;
  }
  public void setHolidayDao(HolidayDao holidayDao) {
    this.holidayDao = holidayDao;
  }
  public OUser getUserObj(int id) {
    return (OUser)this.holidayDao.getObject(new OUser(), id);
  }
  public Long countHolidayTotal(String name, String account, String year, String orgName) {
    String firstDay = DateUtil.getYearFirstDay(Integer.parseInt(year));
    String lastDay = DateUtil.getYearLastDay(Integer.parseInt(year));
    StringBuffer sql = new StringBuffer();
    sql.append("select count(user_id) from(select user_id  from (select l.user_id,l.create_time,u.account,u.name,u.org_id,u.org_name  from o_holiday l,o_user u where l.user_id=u.id");
    sql.append("  and l.create_time between to_date('").append(firstDay).append(" 00:00:00','yyyy-mm-dd hh24:mi:ss')").append(" and to_date('").append(lastDay).append(" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
    if ((account != null) && (!"".equals(account))) {
      sql.append(" and u.account like '%" + account.trim() + "%'");
    }
    if ((name != null) && (!"".equals(name))) {
      sql.append(" and u.name like '%" + name.trim() + "%'");
    }
    if ((orgName != null) && (!"".equals(orgName))) {
      sql.append(" and u.org_name like '%" + orgName.trim() + "%'");
    }
    sql.append(") m group by  m.user_id)");
    BigDecimal total = this.holidayDao.countBySql(sql.toString());
    return Long.valueOf(total.longValue());
  }
  public List<OHoliday> getHolidayTotalList(String name, String account, String year, String orgName, int currentPage) {
    String firstDay = DateUtil.getYearFirstDay(Integer.parseInt(year));
    String lastDay = DateUtil.getYearLastDay(Integer.parseInt(year));
    StringBuffer sql = new StringBuffer();
    sql.append("select user_id,max(name),max(account),sum(days),max(org_id) org from (select l.user_id,l.days,l.create_time,u.account,u.name,u.org_id,u.org_name  from o_holiday l,o_user u where l.user_id=u.id");
    sql.append("  and l.create_time between to_date('").append(firstDay).append(" 00:00:00','yyyy-mm-dd hh24:mi:ss')").append(" and to_date('").append(lastDay).append(" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
    if ((account != null) && (!"".equals(account))) {
      sql.append(" and u.account like '%" + account.trim() + "%'");
    }
    if ((name != null) && (!"".equals(name))) {
      sql.append(" and u.name like '%" + name.trim() + "%'");
    }
    if ((orgName != null) && (!"".equals(orgName))) {
      sql.append(" and u.org_name like '%" + orgName.trim() + "%'");
    }
    sql.append(") m group by  m.user_id  order by  org");
    List holidayList = new ArrayList();
    List<Object[]> list = this.holidayDao.findByPageSql(sql.toString(), currentPage);
    for (Object[] obj : list) {
      OHoliday holiday = new OHoliday();
      holiday.setUserId(Integer.valueOf(Integer.parseInt(obj[0].toString())));
      holiday.setName(obj[1].toString());
      holiday.setAccount(obj[2].toString());
      holiday.setDays(obj[3].toString());
      holiday.setOrgName(getUserAllOrgName(Integer.parseInt(obj[4].toString())));
      holidayList.add(holiday);
    }
    return holidayList;
  }

  public Long countLeaveTotal(String name, String account, String year, String orgName) {
    String firstDay = DateUtil.getYearFirstDay(Integer.parseInt(year));
    String lastDay = DateUtil.getYearLastDay(Integer.parseInt(year));
    StringBuffer sql = new StringBuffer();
    sql.append("select count(user_id) from(select user_id  from (select l.user_id,l.create_time,u.account,u.name,u.org_id,u.org_name  from o_leave l,o_user u where l.user_id=u.id");
    sql.append("  and l.create_time between to_date('").append(firstDay).append(" 00:00:00','yyyy-mm-dd hh24:mi:ss')").append(" and to_date('").append(lastDay).append(" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
    if ((account != null) && (!"".equals(account))) {
      sql.append(" and u.account like '%" + account.trim() + "%'");
    }
    if ((name != null) && (!"".equals(name))) {
      sql.append(" and u.name like '%" + name.trim() + "%'");
    }
    if ((orgName != null) && (!"".equals(orgName))) {
      sql.append(" and u.org_name like '%" + orgName.trim() + "%'");
    }
    sql.append(") m group by  m.user_id) ");
    BigDecimal total = this.holidayDao.countBySql(sql.toString());
    if (total != null) {
      return Long.valueOf(total.longValue());
    }
    return Long.valueOf(0L);
  }
  public List<OLeave> getLeaveTotalList(String name, String account, String year, String orgName, int currentPage) {
    String firstDay = DateUtil.getYearFirstDay(Integer.parseInt(year));
    String lastDay = DateUtil.getYearLastDay(Integer.parseInt(year));
    StringBuffer sql = new StringBuffer();
    sql.append("select user_id,max(name),max(account),sum(days1),sum(days2),max(org_id) org from (select l.user_id,l.days1,l.days2,l.create_time,u.account,u.name,u.org_id,u.org_name  from o_leave l,o_user u where l.user_id=u.id");
    sql.append("  and l.create_time between to_date('").append(firstDay).append(" 00:00:00','yyyy-mm-dd hh24:mi:ss')").append(" and to_date('").append(lastDay).append(" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
    if ((account != null) && (!"".equals(account))) {
      sql.append(" and u.account like '%" + account.trim() + "%'");
    }
    if ((name != null) && (!"".equals(name))) {
      sql.append(" and u.name like '%" + name.trim() + "%'");
    }
    if ((orgName != null) && (!"".equals(orgName))) {
      sql.append(" and u.org_name like '%" + orgName.trim() + "%'");
    }
    sql.append(") m group by m.user_id  order by org");
    List leaveList = new ArrayList();
    List<Object[]> list = this.holidayDao.findByPageSql(sql.toString(), currentPage);
    for (Object[] obj : list) {
      OLeave leave = new OLeave();
      leave.setUserId(Integer.valueOf(Integer.parseInt(obj[0].toString())));
      leave.setName(obj[1].toString());
      leave.setAccount(obj[2].toString());
      leave.setDays1(obj[3].toString());
      leave.setDays2(obj[4].toString());
      leave.setOrgName(getUserAllOrgName(Integer.parseInt(obj[5].toString())));
      leaveList.add(leave);
    }
    return leaveList;
  }

  public Long myHolidayTotal(int userId, int year) {
    String firstDay = DateUtil.getYearFirstDay(year);
    String lastDay = DateUtil.getYearLastDay(year);
    StringBuffer sql = new StringBuffer();
    sql.append("select sum(days) from o_holiday where user_id=").append(userId);
    sql.append(" and create_time between to_date('").append(firstDay).append(" 00:00:00','yyyy-mm-dd hh24:mi:ss')").append(" and to_date('").append(lastDay).append(" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
    BigDecimal total = this.holidayDao.countBySql(sql.toString());
    if (total != null) {
      return Long.valueOf(total.longValue());
    }
    return Long.valueOf(0L);
  }

  public Long myLeaveTotal(int userId) {
    String firstDay = DateUtil.getYearFirstDay(Integer.parseInt(DateUtil.getYear()));
    String lastDay = DateUtil.getYearLastDay(Integer.parseInt(DateUtil.getYear()));
    StringBuffer sql = new StringBuffer();
    sql.append("select sum(days1)+sum(days2) from o_leave where user_id=").append(userId);
    sql.append(" and create_time between to_date('").append(firstDay).append(" 00:00:00','yyyy-mm-dd hh24:mi:ss')").append(" and to_date('").append(lastDay).append(" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
    BigDecimal total = this.holidayDao.countBySql(sql.toString());
    if (total != null) {
      return Long.valueOf(total.longValue());
    }
    return Long.valueOf(0L);
  }

  public OLeave myLeaveTotals(int userId, int year) {
    String firstDay = DateUtil.getYearFirstDay(year);
    String lastDay = DateUtil.getYearLastDay(year);
    OLeave leave = new OLeave();
    StringBuffer sql = new StringBuffer();
    sql.append("select sum(days1),sum(days2) from o_leave where user_id=").append(userId);
    sql.append(" and create_time between to_date('").append(firstDay).append(" 00:00:00','yyyy-mm-dd hh24:mi:ss')").append(" and to_date('").append(lastDay).append(" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
    List list = this.holidayDao.findBySql(sql.toString());
    if ((list != null) && (list.size() > 0)) {
      Object[] obj = (Object[])list.get(0);
      leave.setDays1(isEmpty(obj[0]));
      leave.setDays2(isEmpty(obj[1]));
    }
    return leave;
  }

  private String isEmpty(Object obj) {
    if (obj != null) {
      return obj.toString();
    }
    return "0";
  }

  public String getUserAllOrgName(int orgId) {
    String sql = "select type,name from o_org start with id=" + orgId + 
      "  connect by prior pid=id order by id desc";
    List<Object[]> list = this.holidayDao.findBySql(sql);
    String orgName = "";
    for (Object[] objs : list) {
      if (orgName.equals(""))
        orgName = objs[1].toString();
      else {
        orgName = objs[1].toString() + "." + orgName;
      }
      if (objs[0].equals("1")) {
        break;
      }
    }
    return orgName;
  }
}

/* Location:           /Volumes/work/oa/WEB-INF/classes/
 * Qualified Name:     com.jcrj.service.HolidayService
 * JD-Core Version:    0.6.0
 */